package com.fastfood.controller;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import com.fastfood.model.blo.OrderBLO;
import com.fastfood.model.dao.Report;
import com.fastfood.util.CommonConstants;
import com.opensymphony.xwork2.ActionSupport;

public class ReportAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	
	private OrderBLO orderBLO;
	
	private List<Report> listReport;
	private JFreeChart chart;
	
	private Date startDate;
	private Date endDate;
	
	public ReportAction() {
		orderBLO = new OrderBLO();
		listReport  = new ArrayList<Report>();
	}

	public String excute() {
		return SUCCESS;
	}
	
	public String view() {
		List<Report> temp = orderBLO.getReportsByDate(startDate, endDate);
		if (temp != null) listReport = temp;
		return SUCCESS;
	}
	
	public String addOrder() {
		return SUCCESS;
	}
	
	public String report() {
		List<Report> temp = orderBLO.getReportsByYear(2013);
		if (temp != null) listReport = temp;
		
		double[][] data = new double[1][listReport.size()];
		for (int i = 0; i < listReport.size(); ++i) {
			data[0][i] = listReport.get(i).getGrandTotal();
		}
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Team ", "", data);
		CategoryAxis3D categoryAxis = new CategoryAxis3D("Match");
		ValueAxis valueAxis = new NumberAxis3D("Run");
		BarRenderer3D barRenderer3D = new BarRenderer3D();
		CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, barRenderer3D);
		plot.setOrientation(PlotOrientation.VERTICAL);
		chart = new JFreeChart("Srore Bord", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
		chart.setBackgroundPaint(new Color(249, 231, 236));
		
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		File reportDir = new File(ServletActionContext.getServletContext().getRealPath(CommonConstants.REPORT_IMAGE_DIR));
		if (!reportDir.exists()) reportDir.mkdir();
		File reportImage = new File(reportDir.getPath() + "/report_chart.png");
		try {
			ChartUtilities.saveChartAsPNG(reportImage, chart, 600, 400, info);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public List<Report> getListReport() {
		return listReport;
	}

	public void setListReport(List<Report> listReport) {
		this.listReport = listReport;
	}

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
}
